/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    bool SElevel(vector<int> v)
    {
        bool ret = false;
        if (v[0] % 2 == 1)
        {
            int prev = v[0];
            for (int i = 1; i < v.size(); ++i)
            {
                if (v[i] % 2 == 0 || prev >= v[i])
                    return false;
                prev = v[i];
            }

            ret = true;
        }

        return ret;
    }
     
    bool SOlevel(vector<int> v)
    {
        bool ret = false;
        if (v[0] % 2 == 0)
        {
            int prev = v[0];
            for (int i = 1; i < v.size(); ++i)
            {
                if (v[i] % 2 == 1 || prev <= v[i])
                    return false;
                prev = v[i];
            }

            ret = true;
        }

        return ret;
    }
public:
    bool isEvenOddTree(TreeNode* root) {
       if (nullptr == root->left && nullptr == root->right)
           return root->val % 2 == 1;
       else
       {
           vector<vector<int> > data;
           queue<TreeNode*> tmp;
           tmp.push(root);
           while (!tmp.empty()) 
           {
               vector<int> v;
               int size = tmp.size();
               while (size--)
               {
                   ListNode* parent = tmp.front();
                   tmp.pop();
                   if (parent->left)
                   {
                       tmp.push(parent->left);
                   }
                   if (parent->right)
                   {
                       tmp.push(parent->right);
                   }
                   v.push_back(parent->val);
               }
               data.push_back(v);
           }
           for (int i = 0; i < data.size(); ++i)
           {
               if (i % 2 == 0 && SElevel(data[i]) == false)
                   return false;
               else if (i % 2 == 1 && SOlevel(data[i]) == false)
                   return false;
               else ;
           }
           

           return true;
       }
    }
};
